package com.engine;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.swing.JPanel;

import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.player.events.VideoOutputEventListener;

public class DVDPanel extends JPanel implements MouseListener,ControllerListener,Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private  JPanel contentPane;
	private  Canvas canvas;
	private MediaPlayerFactory mediaPlayerFactory;
	private EmbeddedMediaPlayer mediaPlayer;
	private CanvasVideoSurface videoSurface;
	public DVDPanel()
	{
		canvas = new Canvas();
	    canvas.setBackground(Color.black);
	    contentPane = new JPanel();
	    contentPane.setBackground(Color.black);
	    contentPane.setLayout(new BorderLayout());
	    contentPane.add(canvas, BorderLayout.CENTER);
	    contentPane.setPreferredSize(this.getPreferredSize());
	    contentPane.setMaximumSize(this.getMaximumSize());
	    
	    mediaPlayerFactory = new MediaPlayerFactory(new String[] {"--no-video-title-show"});
	    
		
	    mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
	    videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
	    
	    mediaPlayer.setVideoSurface(videoSurface);
	    
	    mediaPlayer.addVideoOutputEventListener(new VideoOutputEventListener() {
		@Override
		public void videoOutputAvailable(MediaPlayer mediaPlayer, boolean videoOutput) {
			// TODO Auto-generated method stub
			if(videoOutput) {
		          Dimension size = mediaPlayer.getVideoDimension();
		          if(size != null) {
		        	  int iWidth=Integer.parseInt(ReadConfig.GET("VIDEO_WIDTH"));
		        	  int iHeight=Integer.parseInt(ReadConfig.GET("VIDEO_HEIGHT"));
		            canvas.setSize(iWidth, iHeight);
		          }
		        }
		}
	    });
	    this.add(contentPane);
	    startVideo();
	}
	
	public void setSizeVideo(int width,int height)
	{
		canvas.setSize(width, height);
	}
	public void startVideo()
	{
		new Thread(this).start();
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void controllerUpdate(ControllerEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		//mediaPlayer.playMedia("dvd://F:");
		mediaPlayer.playMedia(ReadConfig.GET("VIDEO_FILE"));
	}

	public void stopPlay()
	{
		System.out.println("Stop Playing");
		mediaPlayer.release();
        mediaPlayerFactory.release();
	}
}
